<html>
<head><meta charset="utf-8"><title>Populating mutable reference in functional style · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Populating.20mutable.20reference.20in.20functional.20style.html">Populating mutable reference in functional style</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241092658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Populating%20mutable%20reference%20in%20functional%20style/near/241092658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Populating.20mutable.20reference.20in.20functional.20style.html#241092658">(Jun 02 2021 at 00:31)</a>:</h4>
<p>Is it possible to use iterator adapters / chaining to set values in a pre-allocated mutable reference?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">populate</span><span class="p">(</span><span class="n">buf</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="kt">i32</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">buf</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">{</span><span class="k">return</span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="cm">/* Imperative version that works</span>
<span class="cm">    for i in 0..5 {</span>
<span class="cm">        buf[i] = 2*i as i32;</span>
<span class="cm">    }</span>
<span class="cm">    */</span><span class="w"></span>

<span class="w">    </span><span class="c1">// Is something like this possible?</span>
<span class="w">    </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">i32</span><span class="p">).</span><span class="n">collect</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="241094595"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Populating%20mutable%20reference%20in%20functional%20style/near/241094595" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank Steffahn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Populating.20mutable.20reference.20in.20functional.20style.html#241094595">(Jun 02 2021 at 00:58)</a>:</h4>
<p>Something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">populate</span><span class="p">(</span><span class="n">buf</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="kt">i32</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">buf</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>


<span class="w">    </span><span class="p">(</span><span class="mi">0</span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="n">zip</span><span class="p">(</span><span class="n">buf</span><span class="p">).</span><span class="n">for_each</span><span class="p">(</span><span class="o">|</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">place</span><span class="p">)</span><span class="o">|</span><span class="w"> </span><span class="o">*</span><span class="n">place</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>ought to work.</p>



<a name="241096350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Populating%20mutable%20reference%20in%20functional%20style/near/241096350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Populating.20mutable.20reference.20in.20functional.20style.html#241096350">(Jun 02 2021 at 01:27)</a>:</h4>
<p><code>slice::fill_with</code> may be nicer?</p>



<a name="241097851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Populating%20mutable%20reference%20in%20functional%20style/near/241097851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Populating.20mutable.20reference.20in.20functional.20style.html#241097851">(Jun 02 2021 at 01:54)</a>:</h4>
<p>I'm seeing different errors when trying to compile the <code>fill_with</code> version locally:</p>
<div class="codehilite"><pre><span></span><code>no method named `fill_with` found for mutable reference `&amp;mut [i32]` in the current scope
</code></pre></div>
<p>This is even after adding <code>use std::slice;</code>. Am I missing something else in order for <code>fill_with</code> to be available locally?</p>
<p>The playgound version gives a more helpful <code>Option</code> mismatch error. I'm guessing the playground has some hidden includes / <code>use</code> statements.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">populate</span><span class="p">(</span><span class="n">buf</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="kt">i32</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">buf</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">gen</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">buf</span><span class="p">.</span><span class="n">fill_with</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="n">gen</span><span class="p">.</span><span class="n">next</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<hr>
<p>Also wondering if there's a good way to cancel filling upon receiving the first <code>None</code> (assuming we don't know how many elements will be added until running the closure).</p>



<a name="241097946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Populating%20mutable%20reference%20in%20functional%20style/near/241097946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Populating.20mutable.20reference.20in.20functional.20style.html#241097946">(Jun 02 2021 at 01:56)</a>:</h4>
<p>I would do something like <code>buf.iter_mut().enumerate().take(5).for_each(|(x, p)| *p = 2*x)</code></p>



<a name="241097973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Populating%20mutable%20reference%20in%20functional%20style/near/241097973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Populating.20mutable.20reference.20in.20functional.20style.html#241097973">(Jun 02 2021 at 01:57)</a>:</h4>
<p>You can use <code>try_for_each</code> or an actual for loop if you want cancellation</p>



<a name="241295207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Populating%20mutable%20reference%20in%20functional%20style/near/241295207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Populating.20mutable.20reference.20in.20functional.20style.html#241295207">(Jun 02 2021 at 17:57)</a>:</h4>
<p>What's your rust compiler version? <code>slice::fill_with</code> has been added in 1.50 as an unstable methods and has been stabilized in 1.51. If you have an older compiler it won't work</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>